﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using YHWE.Web.Models;
using JULONG.WEB.Library;

namespace YHWE.Web.Areas.manage.Controllers
{
    public class documentTypeController : Controller
    {
        DataSession<entityDB2Container, documentType> db = new DataSession<entityDB2Container, documentType>();

        //
        // GET: /manage/productType/
        public ActionResult Index() //id为类别
        {
            ViewData.Model = db.query();
            return View();
        }

        //
        // GET: /manage/productType/Create
        public ActionResult Create() //id为类别
        {
            return View();
        }

        //
        // POST: /manage/productType/Create
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            var entity = db.create();

            try
            {
                UpdateModel(entity);
                db.insert(entity);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /manage/productType/Edit/5

        [ValidateInput(false)]
        public ActionResult Edit(int id)
        {
            ViewData.Model = db.get(d => d.ID == id);
            return View();
        }

        //
        // POST: /manage/productType/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var entity = db.get(d => d.ID == id);

            try
            {
                UpdateModel(entity);
                db.save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(entity);
            }
        }

        //
        // GET: /manage/productType/Delete/5

        public ActionResult Delete(int id)
        {
            ViewData.Model = db.get(d => d.ID == id);
            return View();
        }

        //
        // POST: /manage/productType/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var entity = db.get(d => d.ID == id);
            if (entity.document.Any())
            {
                ViewBag.info = "包含文档，不能删除！";
                return View(entity);
            }
            if (entity.requirement) {
                ViewBag.info = "该记录系统标记为不可删除";
                return View(entity);
            }

            try
            {
                db.delete(entity);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ViewBag.info = e.Message;
                return View(entity);
            }

        }

    }
}
